/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: ActionBarMenuView.java 46 2008-05-15 18:09:39Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import albareth.core.entity.Activatable;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.slick.SlickResources;

public class ActionBarMenuView extends IconGridMenuView
{
    public static final Color COLOR_COOLDOWN = new Color(40, 40, 40, 160);
    
    private PlayerCharacter _player;
    
    public ActionBarMenuView(PlayerCharacter player, 
            int cellWidth, int cellHeight, int cellsHoriz, int cellsVert)
    {
        super(cellWidth, cellHeight, cellsHoriz, cellsVert);
        _player = player;
    }

    public void renderMenuItem(BasicMenuFrame menu, MenuItem item, Graphics g,
            int screenX, int screenY, boolean highlighted)
    {
        Activatable activatable = (Activatable)item.getReferencedObject();
        
        if (activatable == null) {
            return; //nothing to render here, button is empty.
        }
        
        //render icon

        Image iconImage = item.getIconImage();
        iconImage.draw(screenX, screenY, _iconScale);
        if (activatable.isActivatable() && !activatable.canActivate(_player)) {
            //iconImage.draw(screenX, screenY, _iconScale, COLOR_DISABLED);
            SlickResources.MENU_ITEM_DISABLED.draw(screenX, screenY, _iconScale);
        }
        if (highlighted) {
            //iconImage.draw(screenX, screenY, _iconScale, COLOR_HOVER);
            SlickResources.MENU_ITEM_SELECTED.draw(screenX, screenY, _iconScale);
        }
        
        
        //OVERLAY WITH COOLDOWN SHADE:
        long cooldownLeft = _player.getCooldownDurationLeft(activatable);
        if (cooldownLeft > 0)
        {
            long cooldownDuration = _player.getCooldownDuration(activatable);
            long elapsed = cooldownDuration - cooldownLeft;
            
            double pct = (double)elapsed / cooldownDuration;

            g.setColor(COLOR_COOLDOWN);
            int h = (int)(pct * _cellSize.height);
            g.fillRect(
                    screenX, screenY + h,
                    _cellSize.width, _cellSize.height - h
            );
        }
        
        //RENDER SHORTCUT-KEY:
        String shortcut = item.getShortcutString();
        if (shortcut != null) {
            g.setFont(SlickResources.SMALL_FONT);
            g.setColor(Color.white);
            g.drawString(shortcut, screenX+5, screenY+1);
        }
        
        //RENDER AMOUNT IF > 1:
        if (activatable.getAmount() > 1)
        {
            g.setFont(SlickResources.SMALL_FONT);
            g.setColor(Color.yellow);
            g.drawString(
                    String.valueOf("x" + activatable.getAmount()),
                    screenX+_cellSize.width-12, screenY+_cellSize.height-12
            );

        }


        
    }

}
